package com.dailybing.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.bingarchive.BingImage;
import com.bingarchive.PublicUtil;
import com.bingarchive.Record;
import com.bingarchive.view.BingImageView;
import com.bingarchive.view.BingImageView.LoadState;
import com.dailybing.R;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;

public class OneByOneBrowse extends AbstractBrowse implements
		CalendarView.OnDateChangeListener, Button.OnClickListener,
		BingImageView.OnLoadStateListener {

	private Button mbtPrevDay = null;
	private Button mbtNextDay = null;
	private TextView mtvToday = null;
	private BingImageView mbivImage = null;
	private TextView mtvCopyright = null;
	private CalendarView mcvChoose = null;

	long mlnMillisecond = 0;
	SimpleDateFormat msdfFull = null;
	SimpleDateFormat msdfMonthDat = null;

	public OneByOneBrowse(Context context) {
		super(context, R.layout.browse_onebyone);

		msdfFull = new SimpleDateFormat(
				this.mContext.getString(R.string.date_format_full));
		msdfMonthDat = new SimpleDateFormat(
				this.mContext.getString(R.string.date_format_monthday));

		mlnMillisecond = new Date().getTime();

		mbtPrevDay = (Button) this.mvgForm.findViewById(R.id.btPrevDay);
		mbtPrevDay.setOnClickListener(this);
		mbtNextDay = (Button) this.mvgForm.findViewById(R.id.btNextDay);
		mbtNextDay.setOnClickListener(this);
		mtvToday = (TextView) this.mvgForm.findViewById(R.id.tvToday);
		mtvCopyright = (TextView) this.mvgForm.findViewById(R.id.tvCopyright);
		mtvCopyright.setVisibility(View.GONE);
		mbivImage = (BingImageView) this.mvgForm.findViewById(R.id.livImage);
//		mbivImage.setCopyrightTextView(mtvCopyright);
		mbivImage.setOnLoadStateListener(this);
		
		mcvChoose = (CalendarView) this.mvgForm.findViewById(R.id.cvChoose);
		mcvChoose.setOnDateChangeListener(this);

		changeDay(0);
	}

	private void changeDay(long lnChangeMillisecond) {
		mlnMillisecond += lnChangeMillisecond;
		changeDay(new Date(mlnMillisecond));
	}

	private void changeDay(Date dtDay) {
		mbivImage.loadImage(dtDay);
	}

	@Override
	public void onClick(View v) {
		if (v == mbtPrevDay) {
			mcvChoose.setDate(mlnMillisecond - PublicUtil.DAY_MILLISECOND);
		} else if (v == mbtNextDay) {
			mcvChoose.setDate(mlnMillisecond + PublicUtil.DAY_MILLISECOND);
		}

	}

	@Override
	public void onSelectedDayChange(CalendarView view, int year, int month,
			int dayOfMonth) {
		Date _dt = new Date(year - 1900, month, dayOfMonth);
		changeDay(_dt);
	}

	@Override
	public void onLoadState(BingImageView biv, LoadState state) {
		Date _dt = biv.getDate();
		if (_dt != null) {
			mlnMillisecond = biv.getDate().getTime();
			mtvToday.setText(msdfFull.format(new Date(mlnMillisecond)));
			mbtPrevDay.setText(msdfMonthDat.format(new Date(mlnMillisecond - PublicUtil.DAY_MILLISECOND)));
			mbtNextDay.setText(msdfMonthDat.format(new Date(mlnMillisecond + PublicUtil.DAY_MILLISECOND)));
		}
	}

}
